$j(document).ready(function () {
	/**这段代码请放到调用categoryTree.js的jsp中
	jCategoryTree = $j('#categoryTree > ul').simpleTree({
		drag:false,
		afterClick:function(node){
			//请编写该方法
			if(typeof(clickTreeItemAction) != 'undefined')clickTreeItemAction();
		},
		afterDblClick:function(node){
			//请编写该方法
			if(typeof(dbClickTreeItemAction) != 'undefined')dbClickTreeItemAction();
		},
		afterMove:function(destination, source, pos){
		},
		afterAjax:function()
		{
		},
		animate:true
		//,docToFolderConvert:true
	})[0];
	$j("#categoryTree").show();
	**/
});


//将传入的jNode [li(jquery object)]构造为JSON对象{id,name,depth,categoryType}
function getCategoryItemJSON(jNode){
	if(jNode.length >0){
		var id=jNode[0].id;
		var name=$j('span ',jNode).attr("categoryName");
		var depth = $j('span ',jNode).attr("depth");
		var categoryType = $j('span ',jNode).attr("categoryType");
		var categoryPath = $j('span ',jNode).attr("categoryPath");
		treeItem={'id':id,'name':name,'depth':depth,'categoryType':categoryType,'categoryPath':categoryPath};
		return treeItem;
	}else{
		return null;
	}
}
//将tree的激活节点向上移动
function moveActiveCategoryUp(jTree){
	var jNode=jTree.getSelected();
	var line=jNode.prev();
	line.insertBefore(line.prev().prev());
	jNode.insertAfter(line);
	var parent=jNode.parent("ul").parent("li[id]");
	refreshChildrenClass(parent);
}
//将tree的激活节点向上移动到最顶
function moveActiveCategoryTop(jTree){
	var jNode=jTree.getSelected();
	var line=jNode.prev();
	var parent=jNode.parent("ul").parent("li[id]");
	var firstNode = parent.find(".line:first");
	if(firstNode.length !=0){
		jNode.insertBefore(firstNode);
		line.insertBefore(jNode);
		refreshChildrenClass(parent);
	}
}
//将tree的激活节点向下移动
function moveActiveCategoryDown(jTree){
	var jNode=jTree.getSelected();
	var line=jNode.prev();
	jNode.insertAfter(jNode.next().next());
	line.insertBefore(jNode);
	var parent=jNode.parent("ul").parent("li[id]");
	refreshChildrenClass(parent);
}

//将tree的激活节点向下移动到最底
function moveActiveCategoryBottom(jTree){
	var jNode=jTree.getSelected();
	var line=jNode.prev();
	var parent=jNode.parent("ul").parent("li[id]");
	var id2 = parent.attr("id");
	var lastNode = jNode.parent("ul").find(">li:last").prev();
	var id = lastNode.attr("id");
	if(lastNode.length !=0){
		line.insertAfter(lastNode);
		jNode.insertAfter(line);
		refreshChildrenClass(parent);
	}
}

//更新父元素的所有子元素li的class,最后一个子元素的class应为line-last
//输入：父元素li的jquery对象
function refreshChildrenClass(jNode){
	var children=jNode.children("ul").children("li");
	var lastChildren=children.slice(children.length-2);
	$j.each(children,function(){
		$j(this).attr("class",$j(this).attr("class").replace("-last",""));
	});
	$j.each(lastChildren,function(){
		$j(this).attr("class",$j(this).attr("class")+"-last");
	});
}
//在当前激活元素node[li]下增加一个node[li]
function addCategoryItemToTree(jTree,itemId,itemName,categoryName,depth,categoryIcon,categoryType){
	var selectedNode=jTree.getSelected();
	if(selectedNode.children("ul").length==0){
		jCategoryTree.convertToFolder(selectedNode);
	}
	if(selectedNode.children('ul.ajax').length!=0){
		//使用ajaxload子文件且当前文件夹未打开则
		return;  //do nothing
	}
	jCategoryTree.addNode(itemId,itemName);
	$j("#"+itemId,$j(jTree)).children("span:first").attr("categoryName",categoryName);
	$j("#"+itemId,$j(jTree)).children("span:first").attr("depth",depth);
	$j("#"+itemId,$j(jTree)).children("span:first").attr("categoryType",categoryType);
	if(categoryIcon)$j("#"+itemId,$j(jTree)).children("span:first").prepend(categoryIcon);
}
//删除当前激活元素
function deleteTreeItem(jTree){
	var selectedNode=jTree.getSelected();
	var parentNode=selectedNode.parents('li:first');
	var otherNodes=selectedNode.siblings("li[id]");
	if(otherNodes.length==0){
		jTree.convertToDoc(parentNode);
	}else{
		jTree.delNode();
	}
}
//设置激活元素 categoryId为li中的id
function activeTreeItem(jTree,categoryId){
	$j(".active",$j(jTree)).attr("class","text");
	$j("#"+categoryId+" >span",$j(jTree)).attr("class","active");
	
}